﻿//编辑对话框
var dlg = new MyAppDialog(600, 400);//关闭对话框
function closeDialog(reload) {
    dlg.close();

    if (reload)
        $("#datagrid").datagrid("reload");
}

var cols = [[
    { field: "Id", title: 'Id', width: 10, hidden: true },
    { field: "ck", title: '', width: 20, checkbox: true },
    { field: "Sort", title: '排序', sortable: true, width: 70, align: 'center' },
    { field: "Deptname", title: '机关科室名称', sortable: true, width: 160, align: 'left' },
    { field: "Remark", title: '备注', sortable: true, width: 240, align: 'left' }
]];

$(document).ready(function () {

    //删除
    $("#btndel").click(function () { del(); });

    //添加
    $("#btnadd").click(function () {
        dlg.setSize(600, 400);
        dlg.setURL("新增科室", baseurl + "Basic/OfficeDept/Edit/0?" + Math.random());
        dlg.show();
    });
    //查询
    $("#btnquery").click(function (evt) {
        evt.preventDefault();
        var params = $("#datagrid").datagrid('options').queryParams;
        params.s = $("#searchkey").val();
        $("#datagrid").datagrid("load");
    });

    $("#searchkey").keydown(function (event) {
        if (event.keyCode == $.ui.keyCode.ENTER) {
            event.preventDefault();
            $("#btnquery").click();
            return false;
        }
    });

    LoadDatagrid();
});

//加载列表数据
function LoadDatagrid() {
    var $dg = $("#datagrid");
    $dg.datagrid({
        title: '科室列表',
        url: baseurl + 'Basic/OfficeDept/DeptListJson',
        fit: true,
        fitColumns: false,
        striped: true,
        nowrap: true,
        idField: 'Id',
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 45, 60, 75],
        rownumbers: true,
        singleSelect: false,
        queryParams: {
            s: ""
        },
        sortName: 'Sort',
        sortOrder: 'asc',
        columns: cols,
        onLoadSuccess: function () {
            //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
            $dg.datagrid("clearSelections");
        },
        onDblClickRow: function (rowIndex, row) {
            dlg.setSize(600, 400);
            dlg.setURL("编辑科室", baseurl + "Basic/OfficeDept/Edit/" + row.Id + "?" + Math.random());
            dlg.show();

        }
    });
}

//删除
function del() {
    var row = $("#datagrid").datagrid('getSelections');
    if (row.length == 0) {
        $.messager.alert("消息", "请选择要删除的数据！");
        return;
    }

    $.messager.confirm("确认", "确认要删除选中的数据吗？", function (r) {
        if (r) {
            var ids = "";
            for (var i = 0; i < row.length; i++) {
                ids += row[i].Id + ",";
            }

            $.ajax({
                type: "post",
                url: baseurl + "Basic/OfficeDept/Delete",
                data: { ids: ids },
                success: function (e) {
                    if (e.Ret == "0") {
                        $("#datagrid").datagrid("reload");
                    } else {
                        $.messager.alert("错误", e.Msg);
                    }
                }
            });
        }
    });
}